﻿/* Copyright (c) 2010 EPiServer AB
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.Globalization;
using System.Text;
using System.Web;
using EPiServer.Configuration;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.Municipality.Extensions;
using EPiServer.Municipality.Web.UI;

namespace EPiServer.Municipality.Templates.MasterPages {
    public partial class Default : MasterPageBase {
        /* *******************************************************************
         * Properties
         * *******************************************************************/
        #region protected string PageTitle
        /// <summary>
        /// Gets the PageTitle of the Default
        /// </summary>
        /// <value></value>
        protected string PageTitle {
            get {
                return PageReference.StartPage.CompareToIgnoreWorkID(CurrentPage.PageLink) ?
                    string.Format("{0} - {1}", Settings.Instance.SiteDisplayName, CurrentPage.PropertyValueWithDefault("Heading", CurrentPage.PageName)) :
                    string.Format("{0} - {1}", CurrentPage.PropertyValueWithDefault("Heading", CurrentPage.PageName), Settings.Instance.SiteDisplayName);
            }
        }
        #endregion
        #region public string CssClass
        /// <summary>
        /// Get/Sets the CssClass of the Default
        /// </summary>
        /// <value></value>
        public string CssClass { get; set; }
        #endregion
        #region protected string TranslateLink
        /// <summary>
        /// Gets the TranslateLink of the Default
        /// </summary>
        /// <value></value>
        protected string TranslateLink {
            get {
                return Server.HtmlEncode("http://translate.google.com/translate?hl=en&sl=sv&u=") + Server.UrlEncode(Settings.Instance.SiteUrl.ToString().TrimEnd(new[] { '/' }) + CurrentPage.GetExternalUrl());
            }
        }
        #endregion
        #region protected string ChangeLanguageLink
        /// <summary>
        /// Gets the ChangeLanguageLink of the Default
        /// </summary>
        /// <value></value>
        protected string ChangeLanguageLink {
            get {
                PageDataCollection languageBranches = DataFactory.Instance.GetLanguageBranches(CurrentPage.PageLink);
                if (languageBranches == null)
                    return String.Empty;
                
                foreach (PageData languageBranch in languageBranches) {
                    if (languageBranch.LanguageID != CurrentPage.LanguageID && LanguageBranch.Load(languageBranch.LanguageID).Enabled) {
                        const string formatLink = "<li><a href=\"{0}\">{1} {2}</a></li>";
                        string langUrl = UriSupport.AddLanguageSelection(languageBranch.LinkURL, languageBranch.LanguageID);
                        string langText = Translate(new CultureInfo(languageBranch.LanguageID).NativeName);
                        return String.Format(formatLink, langUrl, Translate("/municipality/masterpage/langlinkprefix"), langText);
                    }
                }
                return String.Empty;
            }
        }
        #endregion
        #region protected string StartPageLink
        /// <summary>
        /// Gets the StartPageLink of the Default
        /// </summary>
        /// <value></value>
        protected string StartPageLink {
            get {
                return DataFactory.Instance.GetPage(PageReference.StartPage).LinkURL;
            }
        }
        #endregion
        /* *******************************************************************
         * Methods
         * *******************************************************************/
        #region protected string GetClass()
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected string GetClass() {
            if (!string.IsNullOrEmpty(CssClass)) {
                return string.Format("class=\"{0}\"", CssClass);
            }
            return string.Empty;
        }
        #endregion
        #region override protected void OnInit(EventArgs e)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        override protected void OnInit(EventArgs e) {
            InitializeComponent();
            base.OnInit(e);
        }
        #endregion
        #region private void InitializeComponent()
        /// <summary>
        /// 
        /// </summary>
        private void InitializeComponent() {
            Load += Page_Load;
        }
        #endregion
        /* *******************************************************************
         *  Event methods 
         * *******************************************************************/
        #region protected void Page_Load(object sender, EventArgs e)

        /// <summary>
        /// This method is called when the Page's Load event has been fired.
        /// </summary>
        /// <param name="sender">The <see cref="object"/> that fired the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> of the event.</param>
        protected void Page_Load(object sender, EventArgs e) {
            UtilityLinks.DataBind();
            if (HttpContext.Current.IsDebuggingEnabled) {
                Debug.Visible = true;
            } else {
                Release.Visible = true;
            }
            HttpContext.Current.Response.ContentEncoding = new UTF8Encoding(false);
        }
        #endregion
    }
}